package pl.edu.agh.io.gpstracker.server.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import pl.edu.agh.io.gpstracker.client.dto.MessagesDto;
import pl.edu.agh.io.gpstracker.client.dto.TrackDto;
import pl.edu.agh.io.gpstracker.client.dto.TracksequanceDto;
import pl.edu.agh.io.gpstracker.client.dto.UserDto;
import pl.edu.agh.io.gpstracker.service.MessageService;
import pl.edu.agh.io.gpstracker.service.TrackService;
import pl.edu.agh.io.gpstracker.service.UserService;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

@Controller
public class AndroidController {

	private static final String ACCEPT = "Accept";

	private static final String APPLICATION_JSON = "application/json";

	@Autowired
	private UserService userService;

	@Autowired
	private TrackService trackService;

	@Autowired
	private MessageService messageService;

	/**
	 * 
	 * @param login
	 * @param password
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/login.service", method = RequestMethod.GET)
	public void login(
			@RequestParam(value = "login", required = false) String login,
			@RequestParam(value = "password", required = false) String password,
			final HttpServletResponse response

	) throws IOException {
		response.setHeader(ACCEPT, APPLICATION_JSON);	
		UserDto userDto = userService.login(login, password);
		PrintWriter writer = new PrintWriter(response.getOutputStream());
		String responseString = userDto == null ? "" : new JSONSerializer().exclude(
		"*.class").serialize(userDto);
		response.setContentLength(responseString.getBytes().length);
		writer.write(responseString);
		writer.flush();

	}

	/**
	 * 
	 * @param userId
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/readMessages.service ", method = RequestMethod.GET)
	public void readMessages(
			@RequestParam(value = "userId", required = false) Integer userId,
			final HttpServletResponse response) throws IOException {
		response.setHeader(ACCEPT, APPLICATION_JSON);
		List<MessagesDto> messages = messageService.redMessagesByUserid(userId);
		PrintWriter writer = new PrintWriter(response.getOutputStream());
		String responseString = messages == null ? "" : new JSONSerializer().exclude(
		"*.class").serialize(messages);
		response.setContentLength(responseString.getBytes().length);
		writer.write(responseString);
		writer.flush();
	}

	/**
	 * 
	 * @param userId
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/getTrackList.service", method = RequestMethod.GET)
	public void getTrackList(
			@RequestParam(value = "userId", required = false) Integer userId,
			final HttpServletResponse response) throws IOException {
		response.setHeader(ACCEPT, APPLICATION_JSON);
		List<TrackDto> tracks = trackService.findAllByUser(userId);
		// bleeee nie chce mi sie obczajac  o 2 w nocy! o! :P
		for (TrackDto track : tracks){
			track.setTracksequenceList(null);
		}
		PrintWriter writer = new PrintWriter(response.getOutputStream());
		String responseString = tracks == null ? "" : new JSONSerializer().exclude(
				"*.class").serialize(tracks);
		
		response.setContentLength(responseString.getBytes().length);
		writer.write(responseString);
		writer.flush();
	}

	/**
	 * 
	 * @param userId
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addTrackSequence.service", method = RequestMethod.GET)
	public void addSequenceToTrack(
			@RequestParam(value = "trackSequence", required = false) String json,
			final HttpServletResponse response) {
		response.setHeader(ACCEPT, APPLICATION_JSON);
		TracksequanceDto sequence = new JSONDeserializer<TracksequanceDto>()
				.use(null, TracksequanceDto.class).deserialize(json);
		if (null != sequence){
			trackService.addWaySequenceToTrack(sequence);
		}else{
			throw new IllegalArgumentException("Bad Sequence!!!");
		}
	
	}

}
